package com.fhb.demo.myapp.youmeng.ui.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.fhb.demo.myapp.R;
import com.fhb.demo.myapp.youmeng.ui.cosum.CostomConstants;
import com.fhb.demo.myapp.youmeng.ui.receivers.NotificationBroadcast;
import com.umeng.message.UTrack;
import com.umeng.message.common.UmLog;
import com.umeng.message.entity.UMessage;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Random;

/**
 * Created by Administrator on 2018/6/2.
 */

public class MyNotificationService extends BaseService{
    private UMessage oldMsg=null;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(intent==null)
        return super.onStartCommand(intent, flags, startId);
        String msg=intent.getStringExtra("UmengMsg");
        try {
            UMessage umsg=new UMessage(new JSONObject(msg));
            if(oldMsg!=null){
                UTrack.getInstance(ctx).setClearPrevMessage(true);
                UTrack.getInstance(ctx).trackMsgDismissed(oldMsg);
            }
            oldMsg=umsg;
            showNotification(umsg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return super.onStartCommand(intent, flags, startId);
    }
        private void showNotification(UMessage msg) {
            UmLog.e(TAG, "进入===="+TAG);
        int id=new Random(System.nanoTime()).nextInt();
        NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancelAll();
        Notification.Builder mBuilder=new Notification.Builder(ctx);
        mBuilder.setContentTitle(msg.title)
                .setContentText(msg.text)
                .setTicker(msg.ticker)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_launcher)
                .setAutoCancel(true);
        Notification notification=mBuilder.getNotification();
        PendingIntent contentIntent=getContentIntent(ctx,msg);
        PendingIntent deleteIntent=getDeleteIntent(ctx,msg);
        notification.deleteIntent=deleteIntent;
        notification.contentIntent=contentIntent;
        manager.notify(id,notification);


    }

    private PendingIntent getContentIntent(Context ctx, UMessage msg) {
        Intent clickIntent = new Intent();
        clickIntent.setClass(ctx, NotificationBroadcast.class);
        clickIntent.putExtra(CostomConstants.EXTRA_KEY_MSG, msg.getRaw().toString());
        clickIntent.putExtra(CostomConstants.EXTRA_KEY_TYPE, CostomConstants.ACTION_CLICK);
        PendingIntent intent = PendingIntent.getBroadcast(ctx, (int) System.currentTimeMillis(), clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        return intent;
    }
    private PendingIntent getDeleteIntent(Context ctx,UMessage msg){
        Intent clickIntent = new Intent();
        clickIntent.setClass(ctx, NotificationBroadcast.class);
        clickIntent.putExtra(CostomConstants.EXTRA_KEY_MSG, msg.getRaw().toString());
        clickIntent.putExtra(CostomConstants.EXTRA_KEY_TYPE, CostomConstants.ACTION_DISMISS);
        PendingIntent intent = PendingIntent.getBroadcast(ctx, (int) System.currentTimeMillis(), clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        return intent;
    }
}
